package org.masterinftel.scrumpmt.protocol;

import java.util.List;

import org.masterinftel.scrumpmt.Constants;
import org.masterinftel.scrumpmt.R;
import org.masterinftel.scrumpmt.ScrumApplication;
import org.masterinftel.scrumpmt.json.model.DualListColabJSON;
import org.masterinftel.scrumpmt.json.model.TaskColab;
import org.masterinftel.scrumpmt.util.UtilNetwork;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

import com.google.gson.stream.JsonReader;

public class ServerAPI extends Activity {

	private ScrumApplication application;
	private Context ctx;
	private String URLFrontController;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sessionvarview);

		// creamos variables utiles para la aplicacion
		ctx = this;
		application = (ScrumApplication) getApplication();

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		URLFrontController = prefs.getString(Constants.KEY_URL_PREFERENCE, "");

	}

	public void editarColabProducto(List<TaskColab> colabAsociados,
			List<TaskColab> colabNoAsociados, String proyectoID) {
		String urlRest = URLFrontController + ";jsessionid="
				+ application.getSessionID() + "?action=" + Protocol.EDIT_COLAB
				+ "&value=" + proyectoID;

		DualListColabJSON list = new DualListColabJSON();

		list.setColabAsociados(colabAsociados);
		list.setColabNoAsociados(colabNoAsociados);

		JsonReader reader = UtilNetwork.insertJSON(urlRest, list);
		Log.w(Constants.LOGTAG, "ADD_COLAB: " + "Mando: " + list);
	}

	public void addEsfuerzo(int idTarea, int esf) {
		String urlRest = URLFrontController + ";jsessionid="
				+ application.getSessionID() + "?action=" + Protocol.ADD_ESF
				+ "&value=" + idTarea + "&esf=" + esf;

		UtilNetwork.getJSON(urlRest);

		Log.w(Constants.LOGTAG, "ADD_ESF: " + "Mando: " + urlRest);
	}

	public void remTarea(long idTarea) {
		String urlRest = URLFrontController + ";jsessionid="
				+ application.getSessionID() + "?action="
				+ Protocol.REMOVE_TAREA + "&value=" + idTarea;

		UtilNetwork.getJSON(urlRest);

		Log.w(Constants.LOGTAG, "REMOVE_TAREA: " + "Mando: " + urlRest);
	}

	public void addTareaSprint(int idTarea, int idSprint) {
		String urlRest = URLFrontController + ";jsessionid="
				+ application.getSessionID() + "?action="
				+ Protocol.ADD_TAREA_SPRINT + "&value=" + idTarea + "&sprint="
				+ idSprint;

		UtilNetwork.getJSON(urlRest);

		Log.w(Constants.LOGTAG, "ADD_ESF: " + "Mando: " + urlRest);
	}
}